package com.jingxuan.controller;

import com.jingxuan.common.annotation.NoAuth;
import com.jingxuan.common.constant.CategoryConstants;
import com.jingxuan.common.result.Result;
import com.jingxuan.dto.response.CategoryResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类控制器
 */
@Slf4j
@RestController
@RequestMapping("/categories")
public class CategoryController {

    /**
     * 获取所有分类映射
     * GET /api/categories
     */
    @NoAuth
    @GetMapping
    public Result<CategoryResponse.CategoryMappingResponse> getAllCategories() {
        log.info("获取所有分类映射");
        
        CategoryResponse.CategoryMappingResponse response = CategoryResponse.CategoryMappingResponse.builder()
            .bigCategories(CategoryConstants.BIG_CATEGORIES)
            .categoryMapping(CategoryConstants.CATEGORY_MAPPING)
            .build();
        
        return Result.success(response);
    }

    /**
     * 获取所有大类列表
     * GET /api/categories/big
     */
    @NoAuth
    @GetMapping("/big")
    public Result<List<String>> getBigCategories() {
        log.info("获取大类列表");
        return Result.success(CategoryConstants.BIG_CATEGORIES);
    }

    /**
     * 获取指定大类的小类列表
     * GET /api/categories/{bigCategory}/sub
     */
    @NoAuth
    @GetMapping("/{bigCategory}/sub")
    public Result<List<String>> getSubCategories(@PathVariable String bigCategory) {
        log.info("获取大类[{}]的小类列表", bigCategory);
        List<String> subCategories = CategoryConstants.getSubCategories(bigCategory);
        return Result.success(subCategories);
    }

    /**
     * 根据小类获取大类
     * GET /api/categories/find-big
     */
    @NoAuth
    @GetMapping("/find-big")
    public Result<String> findBigCategory(@RequestParam String subCategory) {
        log.info("根据小类[{}]查找大类", subCategory);
        String bigCategory = CategoryConstants.getBigCategory(subCategory);
        return Result.success(bigCategory);
    }
}

